package com.alogic.lucene.analyzer.ik.script;

import com.alogic.lucene.core.ExtAnalyzer;
import org.apache.commons.lang3.StringUtils;
import com.alogic.xscript.AbstractLogiclet;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;

/**
 * 向字典中增加关键字
 * 
 * @author yyduan
 * @since 1.6.11.34
 *
 * @version 1.6.13.8 [20200814 duanyy] <br>
 * - 增加分词的扩展信息(domain,tags)的功能 <br>
 */
public class DicAddWord extends AbstractLogiclet{
	protected String pid = "$indexer-analyzer";
	protected String $word = "";
	protected String $domain = "";
	protected String $tags = "";
	
	public DicAddWord(String tag, Logiclet p) {
		super(tag, p);
	}
	
	@Override
	public void configure(Properties p){
		super.configure(p);
		
		$word = PropertiesConstants.getRaw(p, "word", $word);
		$domain = PropertiesConstants.getRaw(p, "domain", $domain);
		$tags = PropertiesConstants.getRaw(p, "tags", $tags);
	}

	@Override
	protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
		ExtAnalyzer analyzer = ctx.getObject(pid);
		if (analyzer != null){
			String word = PropertiesConstants.transform(ctx, $word, "");
			if (StringUtils.isNotEmpty(word)){
				String domain = PropertiesConstants.transform(ctx,$domain,"");
				if (StringUtils.isEmpty(domain)) {
					analyzer.addWord(word);
				}else{
					String tags = PropertiesConstants.transform(ctx,$tags,"");
					analyzer.addWord(word,domain,tags);
				}
			}
		}else{
			log(String.format("[%s]It is not in a indexer-analyzer context,ignored.",this.getXmlTag()),LOG_WARNING,ctx);
		}
	}
}
